package com.woniuxy.joylifeshop.controller;

import com.woniuxy.common.entity.ShopChat;
import com.woniuxy.common.entity.ShopChatlist;
import com.woniuxy.common.entity.ShopMerchant;
import com.woniuxy.common.utils.JWTUtils;
import com.woniuxy.joylifeshop.dto.ObjectDto;
import com.woniuxy.joylifeshop.service.ChatListService;
import com.woniuxy.joylifeshop.service.ChatService;
import com.woniuxy.joylifeshop.service.MerchantService;
import com.woniuxy.joylifeshop.vo.ResponseInformation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
public class ChatListController {

    @Autowired
    ChatListService chatListService;

    @Autowired
    MerchantService merchantService;

    @Autowired
    ChatService chatService;

    /**
    * @Author SooYoo
    * @Description 创建一个初始化的聊天对象,或者如果他存在,就不管了.
    * @Param
    * @Return
    **/
    @RequestMapping("/createChat")
    public ResponseInformation createChat(String merchantId,HttpServletRequest request){
        String userId = JWTUtils.getUserId(request.getHeader("token"));
        ResponseInformation responseInformation = new ResponseInformation();


        String inf = chatListService.createChatList(merchantId, userId);

        responseInformation.setMsg(inf);
        return responseInformation;
    }

    /**
    * @Author SooYoo
    * @Description 删除一个聊天条目
    * @Param
    * @Return
    **/
    @RequestMapping("/deleteLog")
    public ResponseInformation deleteLog(String listId){
        ResponseInformation responseInformation = new ResponseInformation();

        try {
            chatListService.deleteOneList(listId);
            responseInformation.setMsg("删除条目成功");
            return responseInformation;
        } catch (Exception e) {
            e.printStackTrace();

            responseInformation.setMsg("删除失败");

            return responseInformation;
        }
    }

    /**
    * @Author SooYoo
    * @Description 初始化
    * @Param
    * @Return
    **/
    @RequestMapping("/initList")
    public ResponseInformation initList(HttpServletRequest request){
        String userId = JWTUtils.getUserId(request.getHeader("token"));
        ResponseInformation responseInformation = new ResponseInformation();

        List<ObjectDto> objectDtos = new ArrayList<>();

        try {
            List<ShopChatlist> shopChatlists = chatListService.readChatListByUserId(userId);

            for (ShopChatlist shopChatlist:shopChatlists){
                //聊天列表对象
                ObjectDto objectDto = new ObjectDto();
                objectDto.setData(shopChatlist);
                //找到商家对象
                ShopMerchant shopMerchant = new ShopMerchant();
                shopMerchant.setMerchantId(shopChatlist.getMerchatId());
                ShopMerchant shopMerchantReality = merchantService.readMerchantSimpleOne(shopMerchant);
                objectDto.setData2(shopMerchantReality);
                //聊天内容条目对象
                ShopChat shopChat = chatService.recentOneChatObject(shopChatlist.getMerchatId(), userId);
                objectDto.setData3(shopChat);
                objectDtos.add(objectDto);
            }


            responseInformation.setData(objectDtos);
            return responseInformation;
        } catch (Exception e) {
            e.printStackTrace();
            responseInformation.setMsg("fail");

            return responseInformation;
        }
    }




}
